﻿using System;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using Eassets.Layered.Domain.Secure;

namespace Eassets.Data.Secure
{
    public class ScreenData
    {
        /// <summary>
        /// Inserts screen.
        /// </summary>
        /// <param name="screen"></param>
        public void AddScreen(Screen screen)
        {
            SqlCommand command = new SqlCommand("SP_AddScreen"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, screen));
        }

        /// <summary>
        ///  Updates screen.
        /// </summary>
        /// <param name="screen"></param>
        public void ModifyScreen(Screen screen)
        {
            SqlCommand command = new SqlCommand("SP_ModifyScreen"); // Name of the SP
            command.Parameters.AddWithValue("@ScreenId", screen.ScreenId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, screen));
        }


        /// <summary>
        /// Delete screen.
        /// </summary>
        /// <param name="screenId"></param>
        public void DeleteScreen(long screenId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteScreen"); // Name of the SP
            command.Parameters.AddWithValue("@ScreenId", screenId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId screen.
        /// </summary>
        /// <param name="screenId"></param>
        /// <returns></returns>
        public Screen GetByIdScreen(long screenId)
        {
            SqlCommand command = new SqlCommand("SP_GetScreenById");
            command.Parameters.AddWithValue("@ScreenId", screenId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            Screen screen = new Screen();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["ScreenId"].ToString())))
                {
                    screen.ScreenId = Convert.ToInt64(dataReader["ScreenId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    screen.Description = Convert.ToString(dataReader["Description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EassetModuleId"].ToString())))
                {
                    screen.EassetModuleId = Convert.ToInt64(dataReader["EassetModuleId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ParentScreenId"].ToString())))
                {
                    screen.ParentScreenId = Convert.ToInt64(dataReader["ParentScreenId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ProgramFileName"].ToString())))
                {
                    screen.ProgramFileName = Convert.ToString(dataReader["ProgramFileName"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Sequence"].ToString())))
                {
                    screen.Sequence = Convert.ToInt16(dataReader["Sequence"]);
                }
            }
            dataReader.Close();
            return screen;
        }

        /// <summary>
        /// GetAll screen.
        /// </summary>
        /// <returns></returns>
        public Collection<Screen> GetAllScreen()
        {
            Collection<Screen> screens = new Collection<Screen>();
            SqlCommand command = new SqlCommand("SP_GetAllScreens");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            Screen screen;
            while (dataReader.Read())
            {
                screen = new Screen();
                if (!(String.IsNullOrEmpty(dataReader["ScreenId"].ToString())))
                {
                    screen.ScreenId = Convert.ToInt64(dataReader["ScreenId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Description"].ToString())))
                {
                    screen.Description = Convert.ToString(dataReader["Description"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["EassetModuleId"].ToString())))
                {
                    screen.EassetModuleId = Convert.ToInt64(dataReader["EassetModuleId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ParentScreenId"].ToString())))
                {
                    screen.ParentScreenId = Convert.ToInt16(dataReader["ParentScreenId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ProgramFileName"].ToString())))
                {
                    screen.ProgramFileName = Convert.ToString(dataReader["ProgramFileName"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Sequence"].ToString())))
                {
                    screen.Sequence = Convert.ToInt16(dataReader["Sequence"]);
                }

                screens.Add(screen);
            }
            dataReader.Close();
            return screens;
        }

        //        public Collection<Screen> GetScreenByModuleId(long ModuleId) 
        //        {
        //            Collection<Screen> screens=new Collection<Screen>();

        //screens=
        //            return screens;
        //        }

        public SqlCommand ConstructCommand(SqlCommand command, Screen screen)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@ScreenId", screen.ScreenId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Description", screen.Description);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@EassetModuleId", screen.EassetModuleId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ParentScreenId", screen.ParentScreenId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ProgramFileName", screen.ProgramFileName);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Sequence", screen.Sequence);
            }
            catch (Exception)
            { }
            return command;
        }
    }
}

